package programManager;

import codesound.analysis.*;
import codesound.node.*;
import java.util.ArrayList;

public class Translation extends DepthFirstAdapter {

    //Track pista = Helper.sq.createTrack();
    String comp_error = "";
    //boolean sys = true; //Si este valor esta en true el sistema que se usara es el romance. false es ingles
    boolean ame_flag = false;
    boolean ame_eflag = false;
    boolean rom_flag = false;
    boolean rom_eflag = false;
    boolean oct_flag = false;
    public static boolean move = true;
    int[] value = new int[2];
    int octave = 0;
    int octaveTemp = 0;
    int counter1 = 0;
    int counter2 = 0;
    ArrayList<int[]> noteList;

    @Override
    public void caseStart(Start node) {
        inStart(node);
        node.getPProg().apply(this);
        node.getEOF().apply(this);
        outStart(node);
    }

    @Override
    public void caseAProg(AProg node) {
        inAProg(node);
        if (node.getListProdlist() != null) {
            node.getListProdlist().apply(this);
        }
        outAProg(node);
    }

    @Override
    public void caseAListProdlist(AListProdlist node) {
        inAListProdlist(node);
        if (node.getListConfigprod() != null) {
            node.getListConfigprod().apply(this);
        }
        if (node.getListSpecprod() != null) {
            node.getListSpecprod().apply(this);
            if (rom_eflag) {
                System.out.println("Error: no se perminten notas del sistema romance");
                move = false;
            }
            if (ame_eflag) {
                System.out.println("Error: no se perminten notas del sistema americano");
                move = false;
            }
        }
        if (node.getPCreate() != null) {
            node.getPCreate().apply(this);
        }

        outAListProdlist(node);
    }

    @Override
    public void caseAListListConfigprod(AListListConfigprod node) {
        inAListListConfigprod(node);
        if (node.getPConfigprod() != null) {
            node.getPConfigprod().apply(this);
        }
        if (node.getSemicolon() != null) {
            node.getSemicolon().apply(this);
        }
        if (node.getListConfigprod() != null) {
            node.getListConfigprod().apply(this);
        }
        outAListListConfigprod(node);
    }

    @Override
    public void caseASingleprodListConfigprod(ASingleprodListConfigprod node) {
        inASingleprodListConfigprod(node);
        if (node.getPConfigprod() != null) {
            node.getPConfigprod().apply(this);
        }
        if (node.getSemicolon() != null) {
            node.getSemicolon().apply(this);
        }
        outASingleprodListConfigprod(node);
    }

    @Override
    public void caseASpecprod1PConfigprod(ASpecprod1PConfigprod node) {
        inASpecprod1PConfigprod(node);
        if (node.getPConfigtempo() != null) {
            node.getPConfigtempo().apply(this);
        }
        outASpecprod1PConfigprod(node);
    }

    @Override
    public void caseASpecprod2PConfigprod(ASpecprod2PConfigprod node) {
        inASpecprod2PConfigprod(node);
        if (node.getPConfigcompas() != null) {
            node.getPConfigcompas().apply(this);
        }
        outASpecprod2PConfigprod(node);
    }

    @Override
    public void caseASpecprod3PConfigprod(ASpecprod3PConfigprod node) {
        inASpecprod3PConfigprod(node);
        if (node.getPConfigvoice() != null) {
            node.getPConfigvoice().apply(this);
        }
        outASpecprod3PConfigprod(node);
    }

    @Override
    public void caseASpecprod4PConfigprod(ASpecprod4PConfigprod node) {
        inASpecprod4PConfigprod(node);
        if (node.getPConfigoctave() != null) {
            node.getPConfigoctave().apply(this);
        }
        outASpecprod4PConfigprod(node);
    }

    @Override
    public void caseASpecprod5PConfigprod(ASpecprod5PConfigprod node) {
        inASpecprod5PConfigprod(node);
        if (node.getPConfigsystem() != null) {
            node.getPConfigsystem().apply(this);
        }
        outASpecprod5PConfigprod(node);
    }

    @Override
    public void caseAPConfigtempo(APConfigtempo node) {
        inAPConfigtempo(node);
        if (node.getTempo() != null) {
            node.getTempo().apply(this);
        }
        if (node.getColon() != null) {
            node.getColon().apply(this);
        }
        if (node.getTempoParam() != null) {
            Helper.setTempo(Integer.parseInt(node.getTempoParam().getText().trim()));
        }
        outAPConfigtempo(node);
    }

    @Override
    public void caseAPConfigcompas(APConfigcompas node) {
        float var = 0;
        inAPConfigcompas(node);

        if (node.getNum() != null) {
            if (node.getNum().getText().trim().equals("0")) {
                System.out.println("Error no se permite cero en el compas");
                move = false;
            } else {
                var = Float.parseFloat(node.getNum().getText().trim());
                if (node.getMetric() != null) {
                    Helper.setCompas(var / (Float.parseFloat(node.getMetric().getText().trim().substring(1))));
                }
            }
        }
        outAPConfigcompas(node);
    }

    @Override
    public void caseAPConfigvoice(APConfigvoice node) {
        inAPConfigvoice(node);
        if (node.getVoice() != null) {
            node.getVoice().apply(this);
        }
        if (node.getColon() != null) {
            node.getColon().apply(this);
        }
        if (node.getVoiceParam() != null) {
            node.getVoiceParam().apply(this);
        }
        outAPConfigvoice(node);
    }

    @Override
    public void caseAPConfigoctave(APConfigoctave node) {
        inAPConfigoctave(node);
        if (node.getNum() != null) {
            if (node.getNum().getText().trim().equals("0")) {
                System.out.println("Error: Octava no puede ser cero");
                move = false;
            } else {
                Helper.octave = Integer.parseInt(node.getNum().getText().trim());
            }
            //node.getNum().apply(this);
        }
        outAPConfigoctave(node);
    }

    @Override
    public void caseAPConfigsystem(APConfigsystem node) {
        inAPConfigsystem(node);

        if (node.getSysdef() != null) {
            if (!node.getSysdef().getText().trim().equals("romance")) {
                ame_flag = true;
            } else {
                rom_flag = true;
            }
        }
        outAPConfigsystem(node);
    }

    @Override
    public void caseAList2ListSpecprod(AList2ListSpecprod node) {
        inAList2ListSpecprod(node);
        if (node.getPSpecprod() != null) {
            node.getPSpecprod().apply(this);
        }
        if (node.getSemicolon() != null) {
            node.getSemicolon().apply(this);
        }
        if (node.getListSpecprod() != null) {
            node.getListSpecprod().apply(this);
        }
        outAList2ListSpecprod(node);
    }

    @Override
    public void caseASingleprod2ListSpecprod(ASingleprod2ListSpecprod node) {
        inASingleprod2ListSpecprod(node);
        if (node.getPSpecprod() != null) {
            node.getPSpecprod().apply(this);
        }
        if (node.getSemicolon() != null) {
            node.getSemicolon().apply(this);
        }
        outASingleprod2ListSpecprod(node);
    }

    @Override
    public void caseASpecPSpecprod(ASpecPSpecprod node) {
        inASpecPSpecprod(node);
        if (node.getPSpecline() != null) {
            node.getPSpecline().apply(this);
            octaveTemp = 0;
        }
        outASpecPSpecprod(node);
    }

    @Override
    public void caseASpec2PSpecprod(ASpec2PSpecprod node) {
        inASpec2PSpecprod(node);
        if (node.getPSpecloop() != null) {
            node.getPSpecloop().apply(this);
            octaveTemp = 0;
        }
        outASpec2PSpecprod(node);
    }

    @Override
    public void caseASpec3PSpecprod(ASpec3PSpecprod node) {
        inASpec3PSpecprod(node);
        if (node.getPSpecchord() != null) {
            node.getPSpecchord().apply(this);
        }
        outASpec3PSpecprod(node);
    }

    @Override
    public void caseANormalPSpecline(ANormalPSpecline node) {
        inANormalPSpecline(node);
        noteList = new ArrayList<int[]>();
        if (node.getIdentifier() != null) {
            if (Helper.Dictionary.contains(node.getIdentifier().getText())) {
                {
                    System.out.println("Error: " + node.getIdentifier().getText() + " ya esta siendo usado");
                    move = false;
                }
            } else {
                Helper.Dictionary.add(node.getIdentifier().getText());
                comp_error = node.getIdentifier().getText();
            }
        }
        if (node.getLKey() != null) {
            node.getLKey().apply(this);
        }
        if (node.getPCompaslist() != null) {
            node.getPCompaslist().apply(this);
        }
        if (node.getRKey() != null) {
            node.getRKey().apply(this);
        }
        if (Helper.getCompas() != Helper.getSum()) {
            System.out.println("Error en " + comp_error + ": Algun compas no esta completo");
            //System.out.println("Compas es " + Helper.getCompas());
            //System.out.println("Suma es " + Helper.getSum());
            move = false;
        }
        Helper.noteList.put(comp_error, noteList);
        outANormalPSpecline(node);
    }

    @Override
    public void caseAOctavePSpecline(AOctavePSpecline node) {
        inAOctavePSpecline(node);
        octaveTemp = Helper.getOctave();
        noteList = new ArrayList<int[]>();
        if (node.getLine() != null) {
            node.getLine().apply(this);
        }
        if (node.getIdentifier() != null) {
            if (Helper.Dictionary.contains(node.getIdentifier().getText())) {
                {
                    System.out.println("Error: " + node.getIdentifier().getText() + " ya esta siendo usado");
                    move = false;
                }
            } else {
                Helper.Dictionary.add(node.getIdentifier().getText());
                comp_error = node.getIdentifier().getText();
            }
        }
        if (node.getLPar() != null) {
            node.getLPar().apply(this);
        }
        if (node.getNum() != null) {
            Helper.setOctave(Integer.parseInt(node.getNum().getText().trim()));
        }
        if (node.getRPar() != null) {
            node.getRPar().apply(this);
        }
        if (node.getLKey() != null) {
            node.getLKey().apply(this);
        }
        if (node.getPCompaslist() != null) {
            node.getPCompaslist().apply(this);
        }
        if (node.getRKey() != null) {
            node.getRKey().apply(this);
        }
        if (Helper.getCompas() != Helper.getSum()) {
            System.out.println("Error en " + comp_error + ": Algun compas no esta completo");
            //System.out.println("Compas es " + Helper.getCompas());
            //System.out.println("Suma es " + Helper.getSum());
            move = false;
        }

        Helper.setOctave(octaveTemp);
        Helper.noteList.put(comp_error, noteList);
        outAOctavePSpecline(node);
    }

    /*@Override
    public void caseAPSpecline(APSpecline node)
    {
    inAPSpecline(node);
    if(node.getLine() != null)
    {
    node.getLine().apply(this);
    }
    if(node.getIdentifier() != null)
    {
    if(Helper.Dictionary.contains(node.getIdentifier().getText())){
    System.out.println("Error: " + node.getIdentifier().getText() + " ya esta siendo usado");
    }else
    Helper.Dictionary.add(node.getIdentifier().getText());
    }
    if(node.getLKey() != null)
    {
    node.getLKey().apply(this);
    }
    if(node.getPCompaslist() != null)
    {
    node.getPCompaslist().apply(this);
    }
    if(node.getRKey() != null)
    {
    node.getRKey().apply(this);
    }
    outAPSpecline(node);
    }*/
    @Override
    public void caseANormalPSpecloop(ANormalPSpecloop node) {
        inANormalPSpecloop(node);
        noteList = new ArrayList<int[]>();
        if (node.getIdentifier() != null) {
            if (Helper.Dictionary.contains(node.getIdentifier().getText())) {
                System.out.println("Error: " + node.getIdentifier().getText() + " ya esta siendo usado");
                move = false;
            } else {
                Helper.Dictionary.add(node.getIdentifier().getText());
                comp_error = node.getIdentifier().getText();
            }
        }
        if (node.getLPar() != null) {
            node.getLPar().apply(this);
        }
        if (node.getNum() != null && node.getPCompaslist() != null) {
            if (node.getNum().getText().trim().equals("0")) {
                System.out.println("Error: Loop no puede tener valor 0");
                move = false;
            } else {
                for (int i = 0; i < Integer.parseInt(node.getNum().getText().trim()); i++) {
                    node.getPCompaslist().apply(this);
                }
            }
        }

        if (Helper.getCompas() != Helper.getSum()) {
            System.out.println("Error en " + comp_error + ": Algun compas no esta completo");
            //System.out.println("Compas es " + Helper.getCompas());
            //System.out.println("Suma es " + Helper.getSum());
            move = false;
        }
        Helper.noteList.put(comp_error, noteList);
        outANormalPSpecloop(node);
    }

    @Override
    public void caseAOctavePSpecloop(AOctavePSpecloop node) {
        inAOctavePSpecloop(node);
        octaveTemp = Helper.getOctave();
        noteList = new ArrayList<int[]>();
        if (node.getIdentifier() != null) {
            if (Helper.Dictionary.contains(node.getIdentifier().getText())) {
                System.out.println("Error: " + node.getIdentifier().getText() + " ya esta siendo usado");
                move = false;
            } else {
                Helper.Dictionary.add(node.getIdentifier().getText());
                comp_error = node.getIdentifier().getText();
            }
        }
        if (node.getOctave() != null) {
            Helper.setOctave(Integer.parseInt(node.getOctave().getText().trim()));
        }

        if (node.getRepeat() != null && node.getPCompaslist() != null) {
            if (node.getRepeat().getText().trim().equals("0")) {
                System.out.println("Error: Loop no puede tener valor 0");
                move = false;
            } else {
                for (int i = 0; i < Integer.parseInt(node.getRepeat().getText().trim()); i++) {
                    node.getPCompaslist().apply(this);
                }
            }
        }
        if (node.getRKey() != null) {
            node.getRKey().apply(this);
        }
        if (Helper.getCompas() != Helper.getSum()) {
            System.out.println("Error en " + comp_error + ": Algun compas no esta completo");
            //System.out.println("Compas es " + Helper.getCompas());
            //System.out.println("Suma es " + Helper.getSum());
            move = false;
        }
        Helper.setOctave(octaveTemp);
        Helper.noteList.put(comp_error, noteList);
        outAOctavePSpecloop(node);
    }

    /*@Override
    public void caseAPSpecloop(APSpecloop node)
    {
    inAPSpecloop(node);
    if(node.getLoop() != null)
    {
    node.getLoop().apply(this);
    }
    if(node.getIdentifier() != null)
    {
    if(Helper.Dictionary.contains(node.getIdentifier().getText())){
    System.out.println("Error: " + node.getIdentifier().getText() + " ya esta siendo usado");
    }else
    Helper.Dictionary.add(node.getIdentifier().getText());
    }
    if(node.getLPar() != null)
    {
    node.getLPar().apply(this);
    }
    if(node.getNum() != null)
    {
    if(node.getNum().getText().trim().equals("0"))
    System.out.println("Error: Loop no puede tener valor 0");
    }
    if(node.getRPar() != null)
    {
    node.getRPar().apply(this);
    }
    if(node.getLKey() != null)
    {
    node.getLKey().apply(this);
    }
    if(node.getPCompaslist() != null)
    {
    node.getPCompaslist().apply(this);
    }
    if(node.getRKey() != null)
    {
    node.getRKey().apply(this);
    }
    outAPSpecloop(node);
    }*/
    @Override
    public void caseAPSpecchord(APSpecchord node) {
        inAPSpecchord(node);
        if (node.getChord() != null) {
            node.getChord().apply(this);
        }
        if (node.getIdentifier() != null) {
            if (Helper.Dictionary.contains(node.getIdentifier().getText())) {
                System.out.println("Error: " + node.getIdentifier().getText() + " ya esta siendo usado");
                move = false;
            } else {
                Helper.Dictionary.add(node.getIdentifier().getText());
            }
        }
        if (node.getLKey() != null) {
            node.getLKey().apply(this);
        }
        if (node.getPListnote() != null) {
            PPNote typeNote;
            value = new int[4];
            typeNote = ((APListnote) node.getPListnote()).getFirst();
            addChord(typeNote, 0);
            typeNote = ((APListnote) node.getPListnote()).getSecond();
            addChord(typeNote, 1);
            typeNote = ((APListnote) node.getPListnote()).getThird();
            addChord(typeNote, 2);
            ArrayList<int[]> chord = new ArrayList<int[]>();
            chord.add(value);
            //////////////////////////////////////////////////////////////////////////////////////////////
            //System.out.println(node.getIdentifier().getText().trim() + "  " +value.length);
            Helper.noteList.put(node.getIdentifier().getText().trim(), chord);

        }
        if (node.getRKey() != null) {
            node.getRKey().apply(this);
        }
        outAPSpecchord(node);
    }

    @Override
    public void caseAPListnote(APListnote node) {
        inAPListnote(node);
        if (node.getFirst() != null) {
            node.getFirst().apply(this);
        }
        if (node.getSecond() != null) {
            node.getSecond().apply(this);
        }
        if (node.getThird() != null) {
            node.getThird().apply(this);
        }
        outAPListnote(node);
    }

    @Override
    public void caseACompaslistPCompaslist(ACompaslistPCompaslist node) {
        Helper.setSum(0);
        inACompaslistPCompaslist(node);
        if (node.getPScorelist() != null) {

            node.getPScorelist().apply(this);
            //System.out.println(node.toString());
        }
        if (node.getDoubleLine() != null) {
            node.getDoubleLine().apply(this);
        }
        if (node.getPCompaslist() != null) {
            node.getPCompaslist().apply(this);
        }
        outACompaslistPCompaslist(node);
    }

    @Override
    public void caseAPCompaslist(APCompaslist node) {
        Helper.setSum(0);
        inAPCompaslist(node);
        if (node.getPScorelist() != null) {
            node.getPScorelist().apply(this);
        }
        outAPCompaslist(node);
    }

    @Override
    public void caseAScorelistPScorelist(AScorelistPScorelist node) {
        inAScorelistPScorelist(node);
        //Helper.setSum(0);
        if (node.getPScore() != null) {
            node.getPScore().apply(this);
        }
        if (node.getPScorelist() != null) {
            node.getPScorelist().apply(this);
        }
        outAScorelistPScorelist(node);
    }

    @Override
    public void caseAScorePScorelist(AScorePScorelist node) {
        inAScorePScorelist(node);
        //Helper.setSum(0);
        if (node.getPScore() != null) {
            node.getPScore().apply(this);
        }
        if (Helper.getSum() != Helper.getCompas()) {
            System.out.println("Error estas pendejo");
            move = false;
        }
        outAScorePScorelist(node);
    }

    @Override
    public void caseAScorePScore(AScorePScore node) {
        inAScorePScore(node);
        value = new int[2];
        if (node.getPUpdown() != null) {
            if (ame_flag) {
                System.out.println("Error no se permite up o down en sistema ingles");
                move = false;
            } else {
                node.getPUpdown().apply(this);
            }
        }
        if (node.getPNote() != null) {
            node.getPNote().apply(this);
            //temp[0] = Helper.calculateNumberNote(node.getPNote().getText().trim());
        }
        if (node.getMetric() != null) {
            Helper.addSum(1 / (Float.parseFloat(node.getMetric().getText().trim().substring(1))));
            value[1] = Integer.parseInt(node.getMetric().getText().trim().substring(1));
            //node.getMetric().apply(this);////////////////////////////////////////////////////////////////////
        }
        //System.out.println("Tamaño es: " + value.length);
        //System.out.println("Valores son: " + value[0] + "," + value[1]);
        this.noteList.add(value);
        Helper.setOctave(octave);
        outAScorePScore(node);
    }

    @Override
    public void caseAScore2PScore(AScore2PScore node) {
        inAScore2PScore(node);
        value = new int[2];
        if (node.getPNote() != null) {
            node.getPNote().apply(this);
        }
        if (node.getDot() != null) {
            node.getDot().apply(this);
        }
        if (node.getMetric() != null) {
            Helper.addSum(1 / (Float.parseFloat(node.getMetric().getText().trim().substring(1))));
            value[1] = Integer.parseInt(node.getMetric().getText().trim().substring(1));
            //node.getMetric().apply(this);
        }
        //System.out.println("Tamaño es: " + value.length);
        //System.out.println("Valores son: " + value[0] + "," + value[1]);
        this.noteList.add(value);
        outAScore2PScore(node);
    }

    @Override
    public void caseASpec4PScore(ASpec4PScore node) {
        inASpec4PScore(node);
        value = new int[2];
        if (node.getMetric() != null) {
            Helper.addSum(1 / (Float.parseFloat(node.getMetric().getText().trim().substring(1))));
            value[1] = Integer.parseInt(node.getMetric().getText().trim().substring(1));
            value[0] = 0;
            //node.getMetric().apply(this);
        }
        //System.out.println("Tamaño es: " + value.length);
        //System.out.println("Valores son: " + value[0] + "," + value[1]);
        this.noteList.add(value);
        outASpec4PScore(node);
    }

    @Override
    public void caseASpec5PScore(ASpec5PScore node) {
        inASpec5PScore(node);

        if (node.getIdentifier() != null) {
            if (!Helper.Dictionary.contains(node.getIdentifier().getText())) {
                System.out.println("Error: " + node.getIdentifier().getText() + " no esta definido");
                move = false;
            }
            value = Helper.noteList.get(node.getIdentifier().getText().trim()).get(0);
            //System.out.println(node.getIdentifier().getText().trim()+ " " +value[0]+ " " +value[1]+ " "+value[2]+ " "+value[3]);
        }
        if (node.getDot() != null) {
            node.getDot().apply(this);
        }
        if (node.getMetric() != null) {
            Helper.addSum(1 / (Float.parseFloat(node.getMetric().getText().trim().substring(1))));
            value[3] = Integer.parseInt(node.getMetric().getText().trim().substring(1));
            //node.getMetric().apply(this);
        }
        this.noteList.add(value);
        outASpec5PScore(node);
    }

    @Override
    public void caseAUp1PUpdown(AUp1PUpdown node) {
        inAUp1PUpdown(node);
        octave = Helper.getOctave();
        if (node.getUpDown() != null) {
            if (node.getNum() != null) {
                if (node.getUpDown().getText().trim().equals("up")) {
                    if (octave + Integer.parseInt(node.getNum().getText().trim()) <= 8) {
                        Helper.setOctave(octave + Integer.parseInt(node.getNum().getText().trim()));
                    }
                } else if (octave - Integer.parseInt(node.getNum().getText().trim()) >= 0) {
                    Helper.setOctave(octave - Integer.parseInt(node.getNum().getText().trim()));
                }
            }
        }
        outAUp1PUpdown(node);
    }

    @Override
    public void caseAUp2PUpdown(AUp2PUpdown node) {
        inAUp2PUpdown(node);
        octave = Helper.getOctave();

        if (node.getUpDown() != null) {
            if (node.getUpDown().getText().trim().equals("up")) {
                if ((octave + 1) <= 8) {
                    Helper.setOctave(octave + 1);
                }
            } else if ((octave - 1) >= 0) {
                Helper.setOctave(octave - 1);
            }
        }
        outAUp2PUpdown(node);
    }

    @Override
    public void caseANotePNote(ANotePNote node) {
        inANotePNote(node);
        if (node.getNote() != null) {
            if (ame_flag && isRomantic(node.getNote().getText().trim())) {
                System.out.println("Error: no se permite notas del sistema ingles");
                rom_eflag = true;///////////////////////////////////////////////////////////////////trololololololololololo
                move = false;
            }
            if (rom_flag && !isRomantic(node.getNote().getText().trim())) {
                System.out.println("Error: no se perminten notas del sistema romance");
                ame_flag = true;
                move = false;
            }
            if (ame_flag) {
                Helper.setOctave(Integer.parseInt(node.getNote().getText().trim().charAt(1) + ""));
            }
            //System.out.println("Octave es:  " + node.getNote().getText().trim().charAt(1));
            //System.out.println("Octave vale:  " + Helper.getOctave());
            //System.out.println("Mando; " + node.getNote().getText().trim().charAt(0) + " " + Helper.calculateNumberNote(node.getNote().getText().trim().charAt(0) + ""));
            if (ame_flag && Helper.calculateNumberNote(node.getNote().getText().trim().charAt(0) + "") != 3) {
                value[0] = Helper.calculateNumberNote(node.getNote().getText().trim().charAt(0) + "");
            } else if (Helper.calculateNumberNote(node.getNote().getText().trim()) != 3) {
                value[0] = Helper.calculateNumberNote(node.getNote().getText().trim());
            }
        }
        outANotePNote(node);
    }

    @Override
    public void caseANote1PNote(ANote1PNote node) {
        inANote1PNote(node);
        if (node.getNote() != null) {
            if (node.getSharp() != null) {
                if (ame_flag && isRomantic(node.getNote().getText().trim())) {
                    System.out.println("Error: no se permite notas del sistema ingles");
                    rom_eflag = true;
                    move = false;
                }
                if (rom_flag && !isRomantic(node.getNote().getText().trim())) {
                    System.out.println("Error: no se perminten notas del sistema romance");
                    ame_flag = true;
                    move = false;
                }
                if (ame_flag) {
                    Helper.setOctave(Integer.parseInt(node.getNote().getText().trim().charAt(1) + ""));
                }
                //System.out.println("Octave es:  " + node.getNote().getText().trim().charAt(1));
                //System.out.println("Octave vale:  " + Helper.getOctave());
                //System.out.println("Mando; " + node.getNote().getText().trim().charAt(0) + " " + Helper.calculateNumberNote(node.getNote().getText().trim().charAt(0) + "", '#'));
                if (ame_flag && Helper.calculateNumberNote(node.getNote().getText().trim().charAt(0) + "", '#') != 3) {
                    value[0] = Helper.calculateNumberNote(node.getNote().getText().trim().charAt(0) + "", '#');
                } else if (Helper.calculateNumberNote(node.getNote().getText().trim(), '#') != 3) {
                    value[0] = Helper.calculateNumberNote(node.getNote().getText().trim(), '#');
                } else ;
            }
        }
        outANote1PNote(node);
    }

    @Override
    public void caseANote2PNote(ANote2PNote node) {
        inANote2PNote(node);
        if (node.getNote() != null) {
            if (node.getFlat() != null) {
                if (ame_flag && isRomantic(node.getNote().getText().trim())) {
                    System.out.println("Error: no se permite notas del sistema ingles");
                    rom_eflag = true;
                    move = false;
                }
                if (rom_flag && !isRomantic(node.getNote().getText().trim())) {
                    System.out.println("Error: no se perminten notas del sistema romance");
                    ame_eflag = true;
                    move = false;
                }
                if (ame_flag) {
                    Helper.setOctave(Integer.parseInt(node.getNote().getText().trim().charAt(1) + ""));
                }
                //System.out.println("Octave es:  " + node.getNote().getText().trim().charAt(1));
                //System.out.println("Octave vale:  " + Helper.getOctave());
                //System.out.println("Mando; " + node.getNote().getText().trim().charAt(0) + " " + Helper.calculateNumberNote(node.getNote().getText().trim().charAt(0) + "", '&'));
                if (ame_flag && Helper.calculateNumberNote(node.getNote().getText().trim().charAt(0) + "", '&') != 3) {
                    value[0] = Helper.calculateNumberNote(node.getNote().getText().trim().charAt(0) + "", '&');
                } else if (Helper.calculateNumberNote(node.getNote().getText().trim(), '&') != 3) {
                    value[0] = Helper.calculateNumberNote(node.getNote().getText().trim(), '&');
                } else ;
            }
        }
        outANote2PNote(node);
    }

    @Override
    public void caseAPCreate(APCreate node) {
        inAPCreate(node);
        Helper.init();
        if (node.getCreate() != null) {
            node.getCreate().apply(this);
        }
        if (node.getIdentifier() != null) {
            System.out.println(move);
            if (move) {
                System.out.println("Creando archivo " + node.getIdentifier().getText());
                Helper.fileName = node.getIdentifier().getText().trim();
            } else {
                System.out.println("Archivo no creado");
            }

        }
        if (node.getLKey() != null) {
            node.getLKey().apply(this);
        }
        if (node.getListIdentifier() != null) {
            if (move) {
                System.out.println("Creando archivo " + node.getIdentifier().getText());
                node.getListIdentifier().apply(this);
            } else {
                System.out.println("Archivo no creado");
            }

        }
        if (node.getRKey() != null) {
            node.getRKey().apply(this);
        }
        outAPCreate(node);
    }

    @Override
    public void caseAList4ListIdentifier(AList4ListIdentifier node) {
        int[] note;
        inAList4ListIdentifier(node);
        if (node.getIdentifier() != null && node.getGlue() != null) {
            if (!Helper.Dictionary.contains(node.getIdentifier().getText())) {
                System.out.println("Error " + node.getIdentifier().getText() + " no esta definido");
                move = false;
            } else {
                ArrayList<int[]> notes = Helper.noteList.get(node.getIdentifier().getText().trim());
                if (node.getGlue().getText().trim().equals("^")) {
                    Helper.time = counter1;
                    for (int i = 0; i < notes.size(); i++) {
                        note = notes.get(i);
                        //System.out.println(note.length);
                        if (note.length == 4) {
                            Helper.track[0].add(Helper.createNoteOnEvent(note[0], note[3], counter1));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[3], counter1));
                            Helper.track[0].add(Helper.createNoteOnEvent(note[1], note[3], counter1));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[1], note[3], counter1));
                            Helper.track[0].add(Helper.createNoteOnEvent(note[2], note[3], counter1));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[2], note[3], counter1));
                            counter1 = Helper.gcount;
                            //System.out.println("Notas " + note[0] + " " + note[1]);
                        } else {
                            if (note[0] > 0) {
                                Helper.track[0].add(Helper.createNoteOnEvent(note[0], note[1], counter1));
                                Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[1], counter1));
                                //System.out.println("Notas " + note[0] + " " + note[1]);
                            } else {
                                Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[1], counter1));
                            }
                            counter1 = Helper.gcount;
                        }
                        //System.out.println("Counter1" + counter1);
                    }
                    //System.out.println("Helper " + Helper.time);
                    //counter1 = Helper.time;
                    //System.out.println("Helper " + Helper.time + " " + counter1);
                    //Helper.gcount = counter1;
                    counter2 = (counter2 < Helper.gcount) ? Helper.gcount : counter2;
                    counter1 = Helper.time;
                    Helper.gcount = counter1;
                } else if (node.getGlue().getText().trim().equals("|")) {
                    counter1 = counter2;
                    for (int i = 0; i < notes.size(); i++) {
                        note = notes.get(i);
                        //System.out.println(note.length);
                        if (note.length == 4) {
                            Helper.track[0].add(Helper.createNoteOnEvent(note[0], note[3], counter2));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[3], counter2));
                            Helper.track[0].add(Helper.createNoteOnEvent(note[1], note[3], counter2));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[1], note[3], counter2));
                            Helper.track[0].add(Helper.createNoteOnEvent(note[2], note[3], counter2));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[2], note[3], counter2));
                            counter2 = Helper.gcount;
                            //System.out.println("Notas " + note[0] + " " + note[1]);
                        } else {
                            if (note[0] > 0) {
                                Helper.track[0].add(Helper.createNoteOnEvent(note[0], note[1], counter2));
                                Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[1], counter2));
                                //System.out.println("Notas " + note[0] + " " + note[1]);
                            } else {
                                Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[1], counter2));
                            }
                            counter2 = Helper.gcount;
                        }
                    }

                }

            }

            //else{}

        }
        if (node.getListIdentifier() != null) {
            node.getListIdentifier().apply(this);
        }
        outAList4ListIdentifier(node);
    }

    @Override
    public void caseAListIdentifier(AListIdentifier node) {
        int[] note;
        inAListIdentifier(node);
        if (node.getIdentifier() != null) {
            if (!Helper.Dictionary.contains(node.getIdentifier().getText())) {
                System.out.println("Error " + node.getIdentifier().getText() + " no esta definido");
                move = false;
            } else {
                System.out.println(node.getIdentifier().getText().trim());
                ArrayList<int[]> notes = Helper.noteList.get(node.getIdentifier().getText().trim());
                //System.out.println("Tempo es; " + Helper.tempo);

                // for(int[] r : notes){
                // System.out.println("Tam " + r[0] + " " + r[1]);
                // }
                if (counter2 == Helper.gcount) {
                    counter1 = counter2;
                    for (int i = 0; i < notes.size(); i++) {
                        note = notes.get(i);
                        System.out.println(note.length);
                        if (note.length == 4) {
                            Helper.track[0].add(Helper.createNoteOnEvent(note[0], note[3], counter2));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[3], counter2));
                            Helper.track[0].add(Helper.createNoteOnEvent(note[1], note[3], counter2));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[1], note[3], counter2));
                            Helper.track[0].add(Helper.createNoteOnEvent(note[2], note[3], counter2));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[2], note[3], counter2));
                            counter2 = Helper.gcount;
                            //System.out.println("Notas " + note[0] + " " + note[1]);
                        } else {
                            if (note[0] > 0) {
                                Helper.track[0].add(Helper.createNoteOnEvent(note[0], note[1], counter2));
                                Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[1], counter2));
                                //System.out.println("Notas " + note[0] + " " + note[1]);
                            } else {
                                Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[1], counter2));
                            }
                            counter2 = Helper.gcount;
                            //System.out.println("Counter2 " + counter2);
                        }
                    }

                } else {
                    for (int i = 0; i < notes.size(); i++) {
                        note = notes.get(i);
                        //System.out.println(note.length);
                        if (note.length == 4) {
                            Helper.track[0].add(Helper.createNoteOnEvent(note[0], note[3], counter1));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[3], counter1));
                            Helper.track[0].add(Helper.createNoteOnEvent(note[1], note[3], counter1));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[1], note[3], counter1));
                            Helper.track[0].add(Helper.createNoteOnEvent(note[2], note[3], counter1));
                            Helper.track[0].add(Helper.createNoteOffEvent(note[2], note[3], counter1));
                            counter1 = Helper.gcount;
                            //System.out.println("Notas " + note[0] + " " + note[1]);
                        } else {
                            if (note[0] > 0) {
                                Helper.track[0].add(Helper.createNoteOnEvent(note[0], note[1], counter1));
                                Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[1], counter1));
                                //System.out.println("Notas " + note[0] + " " + note[1]);
                            } else {
                                Helper.track[0].add(Helper.createNoteOffEvent(note[0], note[1], counter1));
                            }
                            counter1 = Helper.gcount;
                            //System.out.println("Counter1" + counter1);
                        }
                    }
                    counter1 = Helper.time;
                }
            }

        }
        outAListIdentifier(node);
    }

    public boolean isEnglish(char c) {
        if (c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' || c == 'F' || c == 'G') {
            return true;
        } else {
            return false;
        }
    }

    public boolean isRomantic(String c) {
        switch (c) {
            case "do":
                return true;
            case "re":
                return true;
            case "mi":
                return true;
            case "fa":
                return true;
            case "sol":
                return true;
            case "la":
                return true;
            case "si":
                return true;
        }
        return false;
    }

    public void addChord(PPNote note, int num) {
        if (note instanceof ANotePNote) {
            if (((ANotePNote) note).getNote() != null) {
                if (ame_flag && isRomantic(((ANotePNote) note).getNote().getText().trim())) {
                    System.out.println("Error: no se permite notas del sistema ingles");
                    rom_eflag = true;
                    move = false;
                }
                if (rom_flag && !isRomantic(((ANotePNote) note).getNote().getText().trim())) {
                    System.out.println("Error: no se perminten notas del sistema romance");
                    ame_flag = true;
                    move = false;
                }
                //System.out.println("Mando; " + node.getNote().getText().trim() + " " + Helper.calculateNumberNote(node.getNote().getText().trim()));
                if (Helper.calculateNumberNote(((ANotePNote) note).getNote().getText().trim()) != 3) {
                    value[num] = Helper.calculateNumberNote(((ANotePNote) note).getNote().getText().trim());
                } else ;

                //value[num] = Helper.calculateNumberNote(((ANotePNote)note).getNote().getText().trim());
            }
        } else if (note instanceof ANote1PNote) {
            if (((ANote1PNote) note).getNote() != null) {
                if (((ANote1PNote) note).getSharp() != null) {
                    if (ame_flag && isRomantic(((ANote1PNote) note).getNote().getText().trim())) {
                        System.out.println("Error: no se permite notas del sistema ingles");
                        rom_eflag = true;
                        move = false;
                    }
                    if (rom_flag && !isRomantic(((ANote1PNote) note).getNote().getText().trim())) {
                        System.out.println("Error: no se perminten notas del sistema romance");
                        ame_eflag = true;
                        move = false;
                    }
                    //System.out.println("Mando; " + node.getNote().getText().trim() + " " + Helper.calculateNumberNote(node.getNote().getText().trim()));
                    if (Helper.calculateNumberNote(((ANote1PNote) note).getNote().getText().trim()) != 3) {
                        value[num] = Helper.calculateNumberNote(((ANote1PNote) note).getNote().getText().trim());
                    } else ;

                    //value[num] = Helper.calculateNumberNote(((ANote1PNote)note).getNote().getText().trim(),'#');
                }
            }
        } else if (note instanceof ANote2PNote) {
            if (((ANote2PNote) note).getNote() != null) {
                if (((ANote2PNote) note).getFlat() != null) {
                    if (ame_flag && isRomantic(((ANote2PNote) note).getNote().getText().trim())) {
                        System.out.println("Error: no se permite notas del sistema ingles");
                        rom_eflag = true;
                        move = false;
                    }
                    if (rom_flag && !isRomantic(((ANote2PNote) note).getNote().getText().trim())) {
                        System.out.println("Error: no se perminten notas del sistema romance");
                        ame_eflag = true;
                        move = false;
                    }
                    //System.out.println("Mando; " + node.getNote().getText().trim() + " " + Helper.calculateNumberNote(node.getNote().getText().trim()));
                    if (Helper.calculateNumberNote(((ANote2PNote) note).getNote().getText().trim()) != 3) {
                        value[num] = Helper.calculateNumberNote(((ANote2PNote) note).getNote().getText().trim());
                    } else ;

                    //value[num] = Helper.calculateNumberNote(((ANote2PNote)note).getNote().getText().trim(), '&');
                }
            }
        }

    }
}